package foodtable;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.daum.mobilead.AdConfig;
import net.daum.mobilead.AdHttpListener;
import net.daum.mobilead.MobileAdView;

import com.andro.chansung.ConstantId;
import com.andro.chansung.ConstantName;
import com.andro.chansung.DB;
import com.andro.chansung.R;

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ADongFoodTableFragment extends Fragment implements OnClickListener, AdHttpListener {
	private Activity activity;
	
	private FoodTabldHandler handler;
	private ListView list;
	private TextView dayDisplay;
	
	private MobileAdView adView = null;
	
	private ProgressDialog pd;
	
	public ADongFoodTableFragment() {
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		activity = getActivity();
		handler = new FoodTabldHandler();
		
		SQLiteDatabase db = activity.openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_WRITEABLE, null);
        DB.removeNoticeTable(db);
        DB.createNoticeTable(db);
        db.close();
		
		View view = inflater.inflate(R.layout.food_table_layout, container, false);
		list = (ListView)view.findViewById(R.id.food_table_list_view);
		
        TextView buildingTitleTv = (TextView)view.findViewById(R.id.food_table_building_title);
        buildingTitleTv.setText("식단[A동]");

		Button mondayBtn 	= (Button)view.findViewById(R.id.food_table_monday_btn);
		Button tuesdayBtn 	= (Button)view.findViewById(R.id.food_table_tuesday_btn);
		Button wednesdayBtn = (Button)view.findViewById(R.id.food_table_wednesday_btn);
		Button thursdayBtn 	= (Button)view.findViewById(R.id.food_table_thursday_btn);
		Button fridayBtn	= (Button)view.findViewById(R.id.food_table_friday_btn);
		Button saturdayBtn	= (Button)view.findViewById(R.id.food_table_saturday_btn);
		Button sundayBtn	= (Button)view.findViewById(R.id.food_table_sunday_btn);
		
		mondayBtn.setOnClickListener(this);
		tuesdayBtn.setOnClickListener(this);
		wednesdayBtn.setOnClickListener(this);
		thursdayBtn.setOnClickListener(this);
		fridayBtn.setOnClickListener(this);
		saturdayBtn.setOnClickListener(this);

		sundayBtn.setVisibility(View.GONE);
		
		AdConfig.setClientId("286eZ2HT136bdb4ae98");
		adView = (MobileAdView)view.findViewById(R.id.adview3);
		adView.setAdListener(this);
		adView.setVisibility(View.VISIBLE);
		
		dayDisplay = (TextView)view.findViewById(R.id.food_table_day_display_tv);
		DainFoodUpdateThread thread = new DainFoodUpdateThread(activity, handler, true, false);
		pd = ProgressDialog.show(activity, "로딩중[A동]", ConstantName.FOOD_TABLE_LOADING_MESSAGE_FOR_ADong);
		thread.start();
		return view;
	}
	
	public void onDestroy() {
		super.onDestroy();
		if( adView != null ) {
			adView.destroy();
			adView = null;
		}
	}

	private void loadFoodTable(ConstantId.FOOD_DAY day) {
		SQLiteDatabase db = getActivity().openOrCreateDatabase(ConstantName.DATABASE_NAME, Context.MODE_WORLD_READABLE, null);
		List<Food> tmpBreakfast	= DB.getMenuFromDainOrADongFoodTableByItsDayAndTime(db, true, day, ConstantId.FOOD_TIME.BREAKFAST);
		List<Food> tmpLunch  	= DB.getMenuFromDainOrADongFoodTableByItsDayAndTime(db, true, day, ConstantId.FOOD_TIME.LUNCH);
		List<Food> tmpDinner 	= DB.getMenuFromDainOrADongFoodTableByItsDayAndTime(db, true, day, ConstantId.FOOD_TIME.DINNER);
		db.close();
		
		if( !tmpBreakfast.isEmpty() && !tmpLunch.isEmpty() && !tmpDinner.isEmpty() ) {
			List<FoodList> foodList = new ArrayList<FoodList>();
			foodList.add(new FoodList(tmpBreakfast));
			foodList.add(new FoodList(tmpLunch));
			foodList.add(new FoodList(tmpDinner));
			
			ADongFoodAdapter adapter = new ADongFoodAdapter(activity, foodList);
			list.setAdapter(adapter);
		} else {
			Toast.makeText(getActivity(), "아직 식단이 제공되지 않습니다", Toast.LENGTH_LONG);
		}
	}
	
	public class FoodTabldHandler extends Handler {
		public void handleMessage(Message msg) {
			loadFoodTable(ConstantId.FOOD_DAY.MONDAY);
	
			Calendar calendar = Calendar.getInstance();
			switch( calendar.get(Calendar.DAY_OF_WEEK) ) {
			case 2 :
				loadFoodTable(ConstantId.FOOD_DAY.MONDAY);
				dayDisplay.setText("월");
				break;
				
			case 3 :
				loadFoodTable(ConstantId.FOOD_DAY.TUESDAY);
				dayDisplay.setText("화");
				break;
				
			case 4 :
				loadFoodTable(ConstantId.FOOD_DAY.WEDNESDAY);
				dayDisplay.setText("수");
				break;
				
			case 5 :
				loadFoodTable(ConstantId.FOOD_DAY.THURSDAY);
				dayDisplay.setText("목");
				break;
				
			case 6 :
				loadFoodTable(ConstantId.FOOD_DAY.FRIDAY);
				dayDisplay.setText("금");
				break;
				
			default :
				loadFoodTable(ConstantId.FOOD_DAY.SATURDAY);
				dayDisplay.setText("토");
				break;
			}
			
			if( pd.isShowing() ) {
				pd.dismiss();
			}
		}
	}

	public void onClick(View v) {
		switch( v.getId() ) {
		case R.id.food_table_monday_btn :
			loadFoodTable(ConstantId.FOOD_DAY.MONDAY);
			dayDisplay.setText("월");
			break;
			
		case R.id.food_table_tuesday_btn :
			loadFoodTable(ConstantId.FOOD_DAY.TUESDAY);
			dayDisplay.setText("화");
			break;
			
		case R.id.food_table_wednesday_btn : 
			loadFoodTable(ConstantId.FOOD_DAY.WEDNESDAY);
			dayDisplay.setText("수");
			break;
			
		case R.id.food_table_thursday_btn :
			loadFoodTable(ConstantId.FOOD_DAY.THURSDAY);
			dayDisplay.setText("목");
			break;
			
		case R.id.food_table_friday_btn :
			loadFoodTable(ConstantId.FOOD_DAY.FRIDAY);
			dayDisplay.setText("금");
			break;
			
		case R.id.food_table_saturday_btn :
			loadFoodTable(ConstantId.FOOD_DAY.SATURDAY);
			dayDisplay.setText("토");
			break;
		}
	}

	public void didDownloadAd_AdListener() {
		// TODO Auto-generated method stub
		
	}

	public void failedDownloadAd_AdListener(int arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}
}
